
********************************************************************************	
cd $pathdata_processed
use terminales_fin, clear

gen predicted_gpa_sd = (predicted_gpa)/(variance_gpa)

rename stdteachers_grades teach
rename stdteachers_grades2 teach2
********************************************************************************	    
global controls teach teach2 stdms_gpa stdms_gpa2 m_ms_gpa ///
		 female ///
		 age age2 ///
		 p_age p_age2 m_mage m_fage ///
		 p_educ p_educ2 m_mbu_kl m_fbu_kl ///
		 ln_p_inc m_mwyrkinnt_c m_fwyrkinnt_c 		
************************************************************************
global partial_gpa i.year_vg3 i.skole_fo

gen all = 1
********************************************************************************
cd $pathtable	
********************************************************************************
label var chance "$widetilde{\textrm{GPA_luck}}$"
label var predicted_gpa "$\textrm{GPA_luck}$"

label var chance_sd "Rescaled $widetilde{\textrm{GPA_luck}}$"
label var predicted_gpa_sd "Rescaled $\textrm{GPA_luck}$"

label var stdexam_grades "Exam grades in 3\textsuperscript{rd} year"
label var stdvide3 "High school GPA"
label var completion "On time HS diploma"
label var hs_gs_c "Ever HS diploma"
label var he_e "Ever higher education"
label var share_available "Share of available HE programs"
label var rank_f_he "Selectivity of HE enrollment"
label var bu_sup "Number of completed years in HE"
label var first_job "Ever employed"
label var ln_wyrkinnt_c1 "First job labor income"
label var job10 "Employed 8 years after the exams"
label var ln_wyrkinnt_c10 "Labor income 8 years after the exams"
*******************************************************************************			
eststo clear
keep if variance_gpa > 0.05 & !missing(variance_gpa)		

areg predicted_gpa_sd chance_sd $controls i.year_vg3 if all == 1, cluster(group) rob absorb(skole_fo)
test chance_sd

//Rescaled GPA luck
foreach sample in all {
	foreach var in stdvide3 {		
				eststo `sample'`var': ivregress 2sls `var' (predicted_gpa_sd = chance_sd) $controls $partial_gpa if `sample' == 1, cluster(group) rob 
				su `var' if e(sample) 
				estadd r(mean): `sample'`var'
				}
			}
foreach sample in all {
	foreach var in job10 ln_wyrkinnt_c10 {		
				eststo `sample'`var': ivregress 2sls `var' (predicted_gpa_sd = chance_sd) $controls $partial_gpa if `sample' == 1, cluster(group) rob 
				su `var' if e(sample) 
				estadd r(mean): `sample'`var'
				}
			}		
foreach sample in all {
	foreach var in ln_wyrkinnt_c10 {		
		
				eststo `sample'`var'iv: ivregress 2sls `var' (stdvide3 = chance_sd) $controls $partial_gpa if `sample' == 1, cluster(group) rob 				
				su `var' if e(sample) 
				estadd r(mean): `sample'`var'iv
				
				}
			}	
			
//Non rescaled GPA luck on the rescaled sample
foreach sample in all {
	foreach var in stdvide3 {		
				eststo `sample'`var'm: ivregress 2sls `var' (predicted_gpa = chance) $controls $partial_gpa if `sample' == 1, cluster(group) rob 
				su `var' if e(sample) 
				estadd r(mean): `sample'`var'm
				}
			}
foreach sample in all {
	foreach var in job10 ln_wyrkinnt_c10 {		
				eststo `sample'`var'm: ivregress 2sls `var' (predicted_gpa = chance) $controls $partial_gpa if `sample' == 1, cluster(group) rob 
				su `var' if e(sample) 
				estadd r(mean): `sample'`var'm
				}
			}		
foreach sample in all {
	foreach var in ln_wyrkinnt_c10 {		
		
				eststo `sample'`var'ivm: ivregress 2sls `var' (stdvide3 = chance) $controls $partial_gpa if `sample' == 1, cluster(group) rob 				
				su `var' if e(sample) 
				estadd r(mean): `sample'`var'ivm
				
				}
			}			
		
		local sample "all"		
		esttab `sample'stdvide3 `sample'ln_wyrkinnt_c10 `sample'ln_wyrkinnt_c10iv using "monotonicity.tex", replace booktabs legend ///
		keep(predicted_gpa_sd stdvide3) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("& First stage: & Reduced form: & 2SLS: \\" "& High school GPA & Annual labor income & Annual labor income \\" "& & 8 years after the exams & 8 years after the exams \\" "& & (log) & (log) \\" "\midrule" "\multicolumn{4}{l}{\textit{Using a rescaled instrument}} \\") ///
		fragment label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
		
		local sample "all"		
		esttab `sample'stdvide3m `sample'ln_wyrkinnt_c10m `sample'ln_wyrkinnt_c10ivm using "monotonicity.tex", append booktabs legend ///
		keep(predicted_gpa stdvide3) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("\midrule" "\multicolumn{4}{l}{\textit{Using a non-rescaled instrument}} \\") ///
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
		
		eststo clear
********************************************************************************	
